package com.android.delivery.adapter.wangdian

import android.widget.ImageView
import android.widget.TextView
import com.android.delivery.R
import com.android.delivery.activity.wangdian.RiderManagementActivity
import com.android.delivery.entity.User
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

/**
 * @author: mqwang
 * @create: 3/20/21 7:46 AM
 * @Description: 骑手管理
 */
class RiderManagementAdapter : BaseQuickAdapter<User, BaseViewHolder>(R.layout.adapter_rider_management) {

    private var mType: Int = 0
    private var mListener: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.mListener = listener
    }

    fun setType(type: Int) {
        mType = type
    }

    override fun convert(helper: BaseViewHolder?, item: User?) {
        helper!!.setText(R.id.tv_name, item!!.realName)
        helper!!.setText(R.id.tv_phone, item!!.mobile)

        if (mType == RiderManagementActivity.TYPE_TRANSFER || mType == RiderManagementActivity.TYPE_DISPATH_TRANSFER) {
            helper.setGone(R.id.tv_select, true)
            helper.itemView.setOnClickListener {
                selectItem(helper.adapterPosition)
            }
        } else {
            helper.setGone(R.id.tv_select, false)
        }

        helper.getView<ImageView>(R.id.tv_select).isSelected = item.isSelect

        helper.getView<TextView>(R.id.tv_bianji).setOnClickListener {
            mListener?.onEdit(item)
        }

        helper.getView<TextView>(R.id.tv_shanchu).setOnClickListener {
            mListener?.onDelete(item)
        }
    }

    fun selectItem(position: Int) {
        for (item in data) {
            if (item.isSelect) {
                item.isSelect = false
            }
        }
        data.get(position).isSelect = true
        notifyDataSetChanged()
    }

    fun getselectItem(): User? {
        for (item in data) {
            if (item.isSelect) {
                return item
            }
        }
        return null
    }

    interface OnItemClickListener {
        fun onDelete(user: User);
        fun onEdit(user: User)
    }
}